define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    is_publish:0,
                    step:1,
                    pid:0,
                    bankList:[],
                    bankSubbranchList:[],
                    contactline:'',
                    mchCacheData:{},
                    industryList:[],
                    accountType:2,
                    type:1,
                    show_dialog_alert:0,
                    uploadUrl:app.params.apiDomain+"apiv2/mch/upload"
                };
            },
            el: '#pageMchAdd',
            created: function() {
                this.fetchData();

            },
            mounted: function() {
                // console.log("123");
                if (this.mounted == 1) {
                    return;
                }
                this.getMchAddCacheData();
                app.areaForbank(".DestinationArea");
                app.areaForbank(".bankArea");
                
                app.MultipleUpload("#indentityPhoto");
                app.upload("#accountCodePhoto");
                app.upload("#mainPhoto");
                app.upload("#licensePhoto");
                app.upload("#gsxt");
                var _this = this;
                app.upload("#handIdcardPhoto",function(){
                    window.photo.uploadUrl = _this.uploadUrl;
                });
                this.mounted = 1;

            },
            methods: {
                fetchData: function() {
                    var _this = this;
                    this.getBank();
                    this.getIndustryList();
                    if( this.$route.params.type==3){
                         this.accountType=1;
                    }
                    this.type = this.$route.params.type;
                    // this.accountType = this.$route.params.type==2?'2':'1';
                } ,
                hide_dialog_alert:function(){
                    this.show_dialog_alert=0;
                },
                dialog_alert:function(){
                    this.show_dialog_alert=1;
                },
               getBank:function(){
                    var that = this;
                    app.get("apiv2/bank/getBank", {
                      
                    }, function (res) {
                      
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.bankList = res.data;
        
                    });
                },
                getBankSubbranch:function(){
                    var that = this;
                    var data={};
                     
                    data.bankId=$("[name='bankId']").val();
                    data.city_code=$("#bankCity").val();
                    app.get("apiv2/bank/getBankSubbranch", data, function (res) {
                      
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.bankSubbranchList = res.data;
                      
                    });
                },
                getIndustryList:function(){
                    var that = this;
                    var data={};
                     
                    data.accountType=this.$route.params.type==3?'企业':'个体工商户';
                    data.shopType=$("#industryShopType").val();
                    app.get("apiv2/industry/get", data, function (res) {
                      
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.industryList = res.data;
                      // that.industryid = $("[name='industryId']").attr("industryId");
                      
                     
                      
                    });
                },
                setContactLine:function(){
                        var value = $("[name='bankName']").val();
                        this.contactline=$("[name='bankName']").find("[value='"+value+"']").attr("contactline");
                },
                pevStep:function(){
                     
                    if(this.step>1){
                        this.step = this.step-1;
                    }
                },
                publish: function() {
                    var _this = this;
                    if (_this.is_publish === 1) {
                        return;
                    }
                     _this.is_publish = 1;
                     
                    var data = $("#pageMchAdd").find("#mchAddForm").serializeArray();
                    data.push({
                        name:"pid",
                        value:this.$route.params.pid
                    });
                    data.push({
                        name:"type",
                        value:this.$route.params.type
                    });
                    app.post("apiv2/mch/add",data,function(res){
                       _this.is_publish =0;
                        if(res.code!=200){
                            app.alert(res.msg);
                            return;
                        }
                        app.setPageData("index",{reload:1});
                        app.alert("提交成功，我们会尽快审核",function(){
                            _this.$router.go(-1);
                        });
                         // _this.$router.push({ name: 'msgSuccess'});
                         // _this.$router.push();

                    });
                    

                },
                mchAddCacheData:function(){
                    var mchAddData =  $("#pageMchAdd").find("#mchAddForm").serializeArray();
                    app.cache().set("mchAddData",mchAddData);
                },
                getMchAddCacheData:function(){
                     var mchAddData = app.cache().get("mchAddData");
                     this.mchAddData = mchAddData;
                     for (var i in mchAddData) {
                         var name = mchAddData[i]['name'];
                          // console.log(name);
                          // console.log(mchAddData[i]['value']);
                         if(mchAddData[i]['value']){
                            $("[name='"+name+"']").val(mchAddData[i]['value']);
                         }
                         if(name=="bankId"){
                             $("[name='"+name+"']").attr("bankId",mchAddData[i]['value']);
                         }
                         if(name=="industryId"){
                             this.industryid =mchAddData[i]['value'];
                         }


                       
                     }
                     
                },
                 
                nextStep:function(){
                    // 校验是否可以下一步
                    if(this.step==1){
                        if($("[name='principal']").val()===""){
                            app.alert("请输入法人姓名");
                            return ;
                        }
                        if($("[name='idCode']").val()===""){
                            app.alert("请输入法人身份证号");
                            return ;
                        }
                        if($("[name='principalMobile']").val()===""){
                            app.alert("请输入手机号");
                            return ;
                        }
                    }
                    // 校验是否可以第二步
                     if(this.step==2){
                        if($("[name='merchantName']").val()===""){
                            app.alert("请输入商户名称");
                            return ;
                        }
                        if($("[name='industryId']").val()===""){
                            app.alert("请选择行业类别");
                            return ;
                        }
                        if($("[name='address']").val()===""){
                            app.alert("请输入详细地址");
                            return ;
                        }
                        if($("[name='province']").val()==="" || $("[name='city']").val()==="" || $("[name='county']").val()===""){
                            app.alert("请选择省市区");
                            return ;
                        }
                        if($("[name='customerPhone']").val()===""){
                            app.alert("请输入客服电话");
                            return ;
                        }
                    }
                     // 校验是否可以第三步
                     if(this.step==3){
                        if($("[name='accountCode']").val()===""){
                            app.alert("请输入银行卡号");
                            return ;
                        }
                        if($("[name='accountName']").val()===""){
                            app.alert("请输入开户人");
                            return ;
                        }
                        if($("[name='bankId']").val()===""){
                            app.alert("请选择银行");
                            return ;
                        }
                        if($("[name='bank_province']").val()==="" || $("[name='bank_city']").val()==="" || $("[name='bank_county']").val()===""){
                            app.alert("请选择银行省市区");
                            return ;
                        }
                        if($("[name='bankName']").val()===""){
                            app.alert("请选择支行");
                            return ;
                        }
                        
                    }
                     // 校验是否可以第四步
                     if(this.step==4){
                        if($(".imgList-name").length===0){
                            app.alert("请上传身份证");
                            return ;
                        }
                        this.mchAddCacheData();
                        this.publish();
                     }
                    
                    if(this.step<4){
                        this.step = this.step+1;
                        this.mchAddCacheData();
                        
                    }
                }

               
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
